﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using StateGrid;
using StateGrid.DLBK;
using StateGrid.User;
using StateGrid.GlobalVariables;
using iTextSharp.text.pdf;
using StateGrid.OperationLog;

public partial class EditPM :  System.Web.UI.Page
{
    /// <summary>
    /// 电力报刊操作对象
    /// </summary>
    private DLBK_DAO dlbkDAO = new DLBK_DAO();
    /// <summary>
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!UserManageDAO.IsLogin((User)Session["login"]))
        {
            GlobalMethod.RedirectLogin(Response);
            return;
        }

        if (!this.IsPostBack)
        {
            this.BindData();
        }

    }

    /// <summary>
    /// 绑定初始化数据
    /// </summary>
    private void BindData()
    {

        String message = null;
        String id = HttpUtility.UrlDecode(Request["id"]);
        if (null == id || "".Equals(id))
        {
            message = "错误：参数错误";
            Response.Redirect(GlobalResource.ERROR_PAGE + "?message=" + message);
            return;
        }
        //ID编号
        int dlbkID = 0;
        try
        {
            dlbkID = Int32.Parse(id);
        }
        catch (Exception)
        {
            message = "错误：参数错误";
            Response.Redirect(GlobalResource.ERROR_PAGE + "?message=" + message);
            return;
        }

        //指定文件名的电力报刊
        DLBK_PM dlbkPM = this.dlbkDAO.GetDLBK_PMByID(dlbkID);

        if (null == dlbkPM)
        {
            message = "错误：没有找到该电力报刊";
            Response.Redirect(GlobalResource.ERROR_PAGE + "?message=" + message);
            return;
        }




        //获取对应的分类信息
        DLBK_FL dlbkFL = this.dlbkDAO.GetDLBK_FLByFlbm(dlbkPM.flbm);

        if (null == dlbkFL)
        {
            message = "错误：没有找到对应的分类编码";
            Response.Redirect(GlobalResource.ERROR_PAGE + "?message=" + message);
            return;
        }


        this.backHyperLink.NavigateUrl = GlobalDLBK.LISTPM_PAGE + "?catalog=" + dlbkFL.flbm;

        //分类编码
        this.flbmTextBox.Text = dlbkFL.flbm;
        //分类名称
        this.flmcTextBox.Text = dlbkFL.flmc;

        //标题
        this.btTextBox.Text = dlbkPM.bt;

        //报告日期
        this.rqTextBox.Text = dlbkPM.rq.ToString();

        //栏目
        this.lmTextBox.Text = dlbkPM.lm;
        //作者
        this.zzTextBox.Text = dlbkPM.zz;
        //登录人
        this.dlrTextBox.Text = dlbkPM.dlr;
        //版号
        this.bhTextBox.Text = dlbkPM.bh.ToString();
        //更新日期
        this.gxrqTextBox.Text = dlbkPM.gxrq.ToString();
        //正文
        this.zwTextBox.Text = dlbkPM.zw;
        this.SetPreAndNextPM();
    }

    /// <summary>
    /// 获取用户填写的电力报刊篇名信息
    /// </summary>
    /// <returns></returns>
    private DLBK_PM GetDLBKPM()
    {
        DLBK_PM dlbkPM = new DLBK_PM();
        //分类编码
        dlbkPM.flbm = this.flbmTextBox.Text;
        //标题
        dlbkPM.bt = this.btTextBox.Text.Trim();
        //作者
        dlbkPM.zz = this.zzTextBox.Text.Trim();
        //日期
        dlbkPM.rq = DateTime.Parse(this.rqTextBox.Text);
        //版号
        dlbkPM.bh = Int32.Parse(this.bhTextBox.Text);
        //栏目
        dlbkPM.lm = this.lmTextBox.Text.Trim();
        //更新日期
        dlbkPM.gxrq = DateTime.Parse(this.rqTextBox.Text);
        //登录人
        dlbkPM.dlr = this.dlrTextBox.Text;
        //正文
        dlbkPM.zw = this.zwTextBox.Text;

        return dlbkPM;

    }

    protected void submitButton_Click(object sender, EventArgs e)
    {
      
        if (!this.ValidateRQ())
        {
            return;
        }
        if (!this.ValidateBT())
        {
            return;
        }
      
        DLBK_PM dlbkPM = this.GetDLBKPM();

        if (this.dlbkDAO.UpdateDLBK_PM(dlbkPM, ((User)Session["login"]).name))
        {
            Response.Write("<Script>alert('修改电力报刊篇名信息成功')</Script>");
            this.NextPM();
        }
        else
        {
            Response.Write("<Script>alert('修改电力报刊篇名信息失败')</Script>");
        }
    }



    /// <summary>
    /// 验证日期
    /// </summary>
    /// <returns></returns>
    private Boolean ValidateRQ()
    {
        Boolean flag = true;

        this.errorLabel.Text = "";
        this.rqTextBox.Text = this.rqTextBox.Text.Trim();
        try
        {
            DateTime.Parse(this.rqTextBox.Text.Trim());
        }
        catch (Exception)
        {
            this.errorLabel.Text = "错误：报告日期不正确";
            flag = false;
        }
        return flag;
    }

    /// <summary>
    /// 验证标题
    /// </summary>
    /// <returns></returns>
    private Boolean ValidateBT()
    {
        Boolean flag = true;

        this.errorLabel.Text = "";
        if (null == this.btTextBox.Text || "".Equals(this.btTextBox.Text.Trim()))
        {
            this.errorLabel.Text = "错误：标题不能为空";
            flag = false;
        }
        return flag;
    }



    protected void zzTextBox_TextChanged(object sender, EventArgs e)
    {
        this.zzTextBox.Text = GlobalMethod.Semicolon(this.zzTextBox.Text.Trim());
    }

    /// <summary>
    /// 设置前一篇和后一篇的链接
    /// </summary>
    private void SetPreAndNextPM()
    {
        //上一篇
        int pre = this.GetDirectionPM(-1);
        if (-1 != pre)
        {
            this.preHyperLink.NavigateUrl = GlobalDLBK.EDITPM_PAGE + "?id=" + pre;
        }

        //下一篇
        int next = this.GetDirectionPM(1);
        if (-1 != next)
        {
            this.nextHyperLink.NavigateUrl = GlobalDLBK.EDITPM_PAGE + "?id=" + next;
        }
    }
    /// <summary>
    /// 修改下一篇
    /// </summary>
    private void NextPM()
    {
        int id = this.GetDirectionPM(1);
        if (-1 == id)
        {
            return;
        }
        else
        {
            Response.Redirect(GlobalJJBK.EDITPM_PAGE + "?id=" + id);
        }
    }

    /// <summary>
    /// 获取下一个文件名，不包括后缀
    /// </summary>
    /// <param name="wjm"></param>
    /// <param name="direction">大于0下一篇；反之，上一篇</param>
    /// <returns></returns>
    private int GetDirectionPM(int direction)
    {
        int id = 0;
        try
        {
            id = Int32.Parse(HttpUtility.UrlDecode(Request["id"]));
        }
        catch (Exception)
        {
            Response.Redirect(GlobalResource.ERROR_PAGE+"?message=错误：系统出错");
        }

        DLBK_PM dlbkPM = this.dlbkDAO.GetDLBK_PMByID(id);
        if (null == dlbkPM)
        {
            return -1;
        }
        return id;
    }
    protected void resetButton_Click(object sender, EventArgs e)
    {
        this.BindData();
    }
}